package spitfire.ksim.algorithm;

public class RequestAlgoFactory {
	enum Algorithms {REPRESENTATIVE, SINGLESENSORABSOLUTE, ACCUMULATIVE, KNN, SUMTOPK}
	
	public static RequestAlgorithm getRequestAlgorithm(String algorithmSpec) {
		String[] splited = algorithmSpec.split(":");
		RequestAlgorithm requestAlgo = null;
		LazyClassificationAlgorithm clsfAlgo = null;
		for (String s : splited) {
			String[] nameAndParams = s.split("-");
			switch (Algorithms.valueOf(nameAndParams[0].toUpperCase())) {
			case REPRESENTATIVE:
				if (clsfAlgo == null) {
					throw new RuntimeException("Specify classification algorithm first");
				}
				requestAlgo = new RepresentativeSelection(clsfAlgo);
				break;
			case SINGLESENSORABSOLUTE:
				if (clsfAlgo == null) {
					throw new RuntimeException("Specify classification algorithm first");
				} else if (nameAndParams.length != 2) {
					throw new RuntimeException("SingleSensorAbsolute should have " +
							"parameter SENSOR_TYPE. Example:\"SingleSensorAbsolute-Light\"");
				}
				requestAlgo = new SingleSensorAbsoluteSelection(nameAndParams[1], clsfAlgo);
				break;
			case ACCUMULATIVE:
				requestAlgo = new AccumulativeSelection();
				break;
			case KNN:
				if (nameAndParams.length != 2) {
					throw new RuntimeException("KNN should have parameter K. " +
							"Example: \"KNN-1\"");
				}
				clsfAlgo = new KNearestNeighbor(Integer.parseInt(nameAndParams[1]));
				break;
			case SUMTOPK:
				if (nameAndParams.length != 2) {
					throw new RuntimeException("SumTopK should have parameter K. " +
							"Example: \"SumTopK-1\"");
				}
				clsfAlgo = new SumTopK(Integer.parseInt(nameAndParams[1]));
				break;
			default:
				throw new RuntimeException("Unknown algorithm name");
			}
		}
		return requestAlgo;
	}
}
